﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;
using Public;

namespace QLNhanKhau
{
    public partial class frmSoHoKhau : DevComponents.DotNetBar.Metro.MetroForm
    {
        public frmSoHoKhau()
        {
            InitializeComponent();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (txtSoNha.Text == ""||txtSoSHK.Text==""||txtDuong.Text==""||txtAp.Text==""||txtHoTen.Text==""||dateNgaySinh.Text==""||txtQueQuan.Text==""||txtNgheNghiep.Text==""
                ||txtCMND.Text==""||txtNoiSinh.Text=="")
            {
                MessageBox.Show("Vui lòng nhập đầy đủ thông tin", "Thông báo nhập liệu", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                return;
            }
            //thêm sổ hộ khẩu
            if (cmbQuanHe.Enabled == false)
            {
                SoHoKhauPublic shk = new SoHoKhauPublic();
                shk.SoSoHoKhau = int.Parse(txtSoSHK.Text);
                shk.SoNha = txtSoNha.Text;
                shk.KhuPho = byte.Parse(txtAp.Text);
                shk.Duong = txtDuong.Text;
                shk.MaSHK = int.Parse(txtMaSHK.Text);

                shk.NgayCap = dateNgayCap.Value;
                shk.NguoiCap = (int)cmbNguoiCap.SelectedValue;
                shk.CanBoDangKi = frmDangNhap.user_login.MaND;
                shk.ChucVuNguoiCap = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue).MaCV;

                SoHoKhauBLL.InsertSoHoKhau(shk);
            }
            //KhaiSinhPublic ks = new KhaiSinhPublic();
            //ks.MaKS = int.Parse(txtMaNK.Text);
            //ks.NoiSinh = txtNoiSinh.Text;
            //KhaiSinhBLL.InsertKhaiSinh(ks);

            //thêm chủ sổ hộ khẩu
            NhanKhauPublic nk = new NhanKhauPublic();
            nk.MaNK = int.Parse(txtMaNK.Text);
            nk.HoTen = txtHoTen.Text;
            //nk.MaKS = ;
            nk.NgaySinh = DateTime.Parse(dateNgaySinh.Text);
            nk.NoiSinh = txtNoiSinh.Text;
            if (rdtGioiTinhNam.Checked)
                nk.GioiTinh = true;
            else
                nk.GioiTinh = false;
            nk.QueQuan = txtQueQuan.Text;
            nk.MaDT = (byte)cmbQuocTich.SelectedValue;
            if (cmbQuanHe.Enabled == true)
            {
                nk.MaQH = (byte)cmbQuanHe.SelectedValue;
            }
            else
            {
                nk.MaQH = 1;
            }  
            nk.NgheNghiep = txtNgheNghiep.Text;
            nk.MaQT = (byte)cmbQuocTich.SelectedValue;
            //nk.MaCT = 0;
            nk.MaTG = (byte)cmbTonGiao.SelectedValue;
            nk.MaTD = (byte)cmbTrinhDo.SelectedValue;
            nk.CMND = txtCMND.Text;
            if (txtTenGoiKhac.Text == "")
            {
                txtTenGoiKhac.Text = null;
            }
            nk.TenGoiKhac = txtTenGoiKhac.Text;
            nk.MaSHK = int.Parse(txtMaSHK.Text);
            //nk.MaKS = int.Parse(txtMaNK.Text);
            //nk.MaKS = null;
            NhanKhauBLL.InsertNhanKhau(nk);

           
            MessageBox.Show("Thêm thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            dgvNhanKhau.DataSource = NhanKhauBLL.LoadNhanKhauByMaSHK(int.Parse(txtMaSHK.Text));
            //load len nhan khẩu
            //dgvNhanKhau.Columns[0].HeaderText = "Mã nhân khẩu";
            dgvNhanKhau.Columns[1].HeaderText = "Họ và tên";
            //dgvNhanKhau.Columns[3].HeaderText = "Tên gọi khác";
            dgvNhanKhau.Columns[4].HeaderText = "CMND";
            dgvNhanKhau.Columns[5].HeaderText = "Ngày sinh";
            //dgvNhanKhau.Columns[6].HeaderText = "Nơi sinh";
            dgvNhanKhau.Columns[7].HeaderText = "Giới tính";
            dgvNhanKhau.Columns[8].HeaderText = "Quê quán";
            dgvNhanKhau.Columns[9].HeaderText = "Dân tộc";
            dgvNhanKhau.Columns[10].HeaderText = "Nghề nghiệp";
            dgvNhanKhau.Columns[11].HeaderText = "Quốc tịch";
            //dgvNhanKhau.Columns[12].HeaderText = "Chứng tử";
            dgvNhanKhau.Columns[13].HeaderText = "Tôn giáo";
            dgvNhanKhau.Columns[14].HeaderText = "Trình độ";
            //dgvNhanKhau.Columns[15].HeaderText = "Sổ hộ khẩu";
            dgvNhanKhau.Columns[16].HeaderText = "Quan hệ";
            dgvNhanKhau.Columns[0].Visible = false;
            //dgvNhanKhau.Columns[1].Visible = false;
            dgvNhanKhau.Columns[2].Visible = false;
            dgvNhanKhau.Columns[3].Visible = false;
            //dgvNhanKhau.Columns[4].Visible = false;
            //dgvNhanKhau.Columns[5].Visible = false;
            dgvNhanKhau.Columns[6].Visible = false;
            //dgvNhanKhau.Columns[7].Visible = false;
            //dgvNhanKhau.Columns[8].Visible = false;
            dgvNhanKhau.Columns[9].Visible = false;
            //dgvNhanKhau.Columns[10].Visible = false;
            dgvNhanKhau.Columns[11].Visible = false;
            dgvNhanKhau.Columns[12].Visible = false;
            dgvNhanKhau.Columns[13].Visible = false;
            dgvNhanKhau.Columns[14].Visible = false;
            dgvNhanKhau.Columns[15].Visible = false;
            //dgvNhanKhau.Columns[16].Visible = false;
            //groupBox3.Enabled = false;
            cmbQuanHe.Enabled = true;
            //groupBox3.Refresh();
            groupBox3.ResetText();
            //groupBox3.Validated
            txtHoTen.Text = string.Empty;
            txtQueQuan.ResetText();
            cmbQuocTich.Refresh();
            cmbTonGiao.Refresh();
            cmbTrinhDo.Refresh();
            cmbDanToc.Refresh();
            txtNgheNghiep.ResetText();
            txtNoiSinh.ResetText();
            txtTenGoiKhac.ResetText();
            txtCMND.ResetText();
            dateNgaySinh.ResetValue();
            txtMaNK.Text = DungChung.LoadMaNK().ToString();
            txtAp.Enabled = false;
            txtSoNha.Enabled = false;
            txtDuong.Enabled = false;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Thoát không lưu", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                this.MdiParent = null;
                this.Dispose();
            }
        }

        private void frmSoHoKhau_Load(object sender, EventArgs e)
        {
            txtMaNK.Text = DungChung.LoadMaNK().ToString();
            txtMaSHK.Text = DungChung.LoadMaSHK().ToString();
            txtSoSHK.Text = DungChung.LoadMaSHK().ToString();

            txtMaNK.Enabled = false;
            txtMaSHK.Enabled = false;
            txtSoSHK.Enabled = false;

            //load dân tộc
            cmbDanToc.DataSource = DanTocBLL.LoadDanToc();
            cmbDanToc.DisplayMember = "TenDanToc";
            cmbDanToc.ValueMember = "MaDT";
            //load quoc tich
            cmbQuocTich.DataSource = QuocTichBLL.LoadQuocTich();
            cmbQuocTich.DisplayMember = "TenQuocTich";
            cmbQuocTich.ValueMember = "MaQT";
            //load tôn giáo 
            cmbTonGiao.DataSource = TonGiaoBLL.LoadTonGiao();
            cmbTonGiao.DisplayMember = "TenTonGiao";
            cmbTonGiao.ValueMember = "MaTG";
            //load trình độ
            cmbTrinhDo.DataSource = TrinhDoBLL.LoadTrinhDo();
            cmbTrinhDo.DisplayMember = "TenTrinhDo";
            cmbTrinhDo.ValueMember = "MaTD";
            //load người cấp
            //cmbNguoiCap.BindingContext = new BindingContext();
            //cmbNguoiCap.DataSource = NguoiDungBLL.LoadNguoiCap();
            //cmbNguoiCap.DisplayMember = "HoTen";
            //cmbNguoiCap.ValueMember = "MaND";
            //load chức vụ
            txtCanBoDangKi.Text = frmDangNhap.user_login.HoTen;
            txtCanBoDangKi.Enabled = false;

            cmbNguoiCap.DataSource = NguoiDungBLL.LoadNguoiCap();
            cmbNguoiCap.DisplayMember = "HoTen";
            cmbNguoiCap.ValueMember = "MaND";
            NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue);
            //.Text = nd.ChucVu;
            txtChucVuNguoiCap.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;
            txtChucVuNguoiCap.Enabled = false;

            dateNgayCap.Value = DateTime.Parse(DateTime.Now.ToString());

            cmbQuanHe.DataSource = QuanHeBLL.LoadQuanHe();
            cmbQuanHe.DisplayMember = "TenQuanHe";
            cmbQuanHe.ValueMember = "MaQH";
            cmbQuanHe.Enabled = false;
            }

        private void txtCMND_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtCMND.Text.Length > 8 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtSoSHK_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtSoSHK.Text.Length > 7 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtAp_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) || txtAp.Text.Length > 1 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void cmbNguoiCap_SelectionChangeCommitted(object sender, EventArgs e)
        {
            NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiCap.SelectedValue);
            //.Text = nd.ChucVu;
            txtChucVuNguoiCap.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;
            txtChucVuNguoiCap.Enabled = false;

            //NguoiDungPublic nd = NguoiDungBLL.LoadNguoiDungByMaND((int)cmbNguoiNhan.SelectedValue);
            ////.Text = nd.ChucVu;
            //txtChucVuNguoiNhan.Text = ChucVuBLL.LoadChucVuByMaCV(nd.MaCV).TenChucVu;
            //txtChucVuNguoiNhan.Enabled = false;
        }

        private void txtSoNha_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtSoNha.Text.Length > 8 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtDuong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtDuong.Text.Length > 49&&!Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtHoTen_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtHoTen.Text.Length > 29 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtTenGoiKhac_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtTenGoiKhac.Text.Length > 29 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtNoiSinh_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtNoiSinh.Text.Length > 49 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtNgheNghiep_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtNgheNghiep.Text.Length > 49 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void txtQueQuan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (txtQueQuan.Text.Length > 49 && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }

        private void btnCapSoMoi_Click(object sender, EventArgs e)
        {
            frmSoHoKhau_Load(sender, e);
        }

    }
}
